using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace DataEntity
{
	public class T_Project_Lease : DBInteractionBase
	{
		#region Class Member Declarations
            private SqlInt32        _project_Lease_TypeID;
			private SqlString		_project_Lease_FirstA3, _project_Lease_FirstB1, _project_Lease_FirstB2, _project_Lease_FirstA2, _project_Lease_FirstA1;
			private SqlGuid			_project_Lease_ID, _project_Lease_MainID;
		#endregion


		public T_Project_Lease()
		{
			// Nothing for now.
		}





		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Lease_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Lease_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_ID));
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Lease_MainID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_MainID));
				cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Lease_FirstA1", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_FirstA1));
				cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Lease_FirstA2", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_FirstA2));
				cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Lease_FirstA3", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_FirstA3));
				cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Lease_FirstB1", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_FirstB1));
				cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Lease_FirstB2", SqlDbType.NVarChar, 20, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_FirstB2));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Lease::Update::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Lease_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Lease_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				_rowsAffected = cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Lease::Delete::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Lease_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Project_Lease");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Lease_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _project_Lease_ID));

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_project_Lease_ID = (Guid)toReturn.Rows[0]["Project_Lease_ID"];
					_project_Lease_MainID = (Guid)toReturn.Rows[0]["Project_Lease_MainID"];
					_project_Lease_FirstA1 = toReturn.Rows[0]["Project_Lease_FirstA1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_Lease_FirstA1"];
					_project_Lease_FirstA2 = toReturn.Rows[0]["Project_Lease_FirstA2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_Lease_FirstA2"];
					_project_Lease_FirstA3 = toReturn.Rows[0]["Project_Lease_FirstA3"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_Lease_FirstA3"];
					_project_Lease_FirstB1 = toReturn.Rows[0]["Project_Lease_FirstB1"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_Lease_FirstB1"];
					_project_Lease_FirstB2 = toReturn.Rows[0]["Project_Lease_FirstB2"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Project_Lease_FirstB2"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Lease::SelectOne::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_t_Project_Lease_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("t_Project_Lease");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				if(_mainConnectionIsCreatedLocal)
				{
					// Open connection.
					_mainConnection.Open();
				}
				else
				{
					if(_mainConnectionProvider.IsTransactionPending)
					{
						cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
					}
				}

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("T_Project_Lease::SelectAll::Error occured.", ex);
			}
			finally
			{
				if(_mainConnectionIsCreatedLocal)
				{
					// Close connection.
					_mainConnection.Close();
				}
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlGuid Project_Lease_ID
		{
			get
			{
				return _project_Lease_ID;
			}
			set
			{
				_project_Lease_ID = value;
			}
		}


		public SqlGuid Project_Lease_MainID
		{
			get
			{
				return _project_Lease_MainID;
			}
			set
			{
				SqlGuid project_Lease_MainIDTmp = (SqlGuid)value;
				if(project_Lease_MainIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_MainID", "Project_Lease_MainID can't be NULL");
				}
				_project_Lease_MainID = value;
			}
		}


        public SqlInt32 Project_Lease_TypeID
        {
            get
            {
                return _project_Lease_TypeID;
            }
            set
            {
                SqlInt32 project_Lease_TypeIDTmp = (SqlInt32)value;
                if (project_Lease_TypeIDTmp.IsNull)
                {
                    throw new ArgumentOutOfRangeException("Project_Lease_TypeID", "Project_Lease_TypeID can't be NULL");
                }
                _project_Lease_TypeID = value;
            }
        }


		public SqlString Project_Lease_FirstA1
		{
			get
			{
				return _project_Lease_FirstA1;
			}
			set
			{
				SqlString project_Lease_FirstA1Tmp = (SqlString)value;
				if(project_Lease_FirstA1Tmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_FirstA1", "Project_Lease_FirstA1 can't be NULL");
				}
				_project_Lease_FirstA1 = value;
			}
		}


		public SqlString Project_Lease_FirstA2
		{
			get
			{
				return _project_Lease_FirstA2;
			}
			set
			{
				SqlString project_Lease_FirstA2Tmp = (SqlString)value;
				if(project_Lease_FirstA2Tmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_FirstA2", "Project_Lease_FirstA2 can't be NULL");
				}
				_project_Lease_FirstA2 = value;
			}
		}


		public SqlString Project_Lease_FirstA3
		{
			get
			{
				return _project_Lease_FirstA3;
			}
			set
			{
				SqlString project_Lease_FirstA3Tmp = (SqlString)value;
				if(project_Lease_FirstA3Tmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_FirstA3", "Project_Lease_FirstA3 can't be NULL");
				}
				_project_Lease_FirstA3 = value;
			}
		}


		public SqlString Project_Lease_FirstB1
		{
			get
			{
				return _project_Lease_FirstB1;
			}
			set
			{
				SqlString project_Lease_FirstB1Tmp = (SqlString)value;
				if(project_Lease_FirstB1Tmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_FirstB1", "Project_Lease_FirstB1 can't be NULL");
				}
				_project_Lease_FirstB1 = value;
			}
		}


		public SqlString Project_Lease_FirstB2
		{
			get
			{
				return _project_Lease_FirstB2;
			}
			set
			{
				SqlString project_Lease_FirstB2Tmp = (SqlString)value;
				if(project_Lease_FirstB2Tmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("Project_Lease_FirstB2", "Project_Lease_FirstB2 can't be NULL");
				}
				_project_Lease_FirstB2 = value;
			}
		}
		#endregion
	}
}
